package com.ccc.hotsearch.controller;

import com.ccc.hotsearch.dto.HotSearchData;
import com.ccc.hotsearch.dto.HotSearchResponse;
import com.ccc.hotsearch.dto.PlatformData;
import com.ccc.hotsearch.model.HotItem;
import com.ccc.hotsearch.model.PlatformTypeEnum;
import com.ccc.hotsearch.service.HotSearchManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/hotsearch")
@CrossOrigin(origins = "*")
public class HotSearchController {

    @Autowired
    private HotSearchManager hotSearchManager;

    /**
     * 获取支持的平台列表
     */
    @GetMapping("/platforms")
    public HotSearchResponse getSupportedPlatforms() {
        try {
            Map<String, Object> responseData = new HashMap<>();
            List<Map<String, String>> platforms = Arrays.stream(PlatformTypeEnum.values())
                    .map(pt -> {
                        Map<String, String> platformInfo = new HashMap<>();
                        platformInfo.put("code", pt.getCode());
                        platformInfo.put("name", pt.getName());
                        platformInfo.put("color", pt.getColor());
                        return platformInfo;
                    })
                    .collect(Collectors.toList());

            responseData.put("platforms", platforms);
            responseData.put("total", platforms.size());

            return HotSearchResponse.success(responseData);
        } catch (Exception e) {
            return HotSearchResponse.error("获取平台列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有平台热搜
     */
    @GetMapping("/all")
    public HotSearchResponse getAllHotSearch() {
        try {
            HotSearchData data = hotSearchManager.getAllHotSearch();
            return HotSearchResponse.success(data);
        } catch (Exception e) {
            return HotSearchResponse.error("获取热搜数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取指定平台热搜
     */
    @GetMapping("/platform/{platform}")
    public HotSearchResponse getPlatformHotSearch(@PathVariable String platform) {
        try {
            PlatformTypeEnum platformType = PlatformTypeEnum.valueOf(platform.toUpperCase());
            PlatformData data = hotSearchManager.getHotSearchByPlatform(platformType);

            if (data != null) {
                HotSearchData responseData = new HotSearchData();
                responseData.setPlatforms(Collections.singletonList(data));
                return HotSearchResponse.success(responseData);
            } else {
                return HotSearchResponse.error("平台不存在或暂不支持");
            }
        } catch (IllegalArgumentException e) {
            return HotSearchResponse.error("不支持的平台: " + platform);
        } catch (Exception e) {
            return HotSearchResponse.error("获取平台热搜失败: " + e.getMessage());
        }
    }

    /**
     * 搜索热搜内容
     */
    @GetMapping("/search")
    public HotSearchResponse searchHotSearch(@RequestParam String q) {
        try {
            List<HotItem> results = hotSearchManager.searchHotSearch(q);

            PlatformData platformData = new PlatformData();
            platformData.setPlatform(PlatformTypeEnum.WEIBO);
            platformData.setItems(results);
            platformData.setTotal(results.size());
            platformData.setUpdateTime(System.currentTimeMillis());
            platformData.setNextUpdate("实时");

            HotSearchData data = new HotSearchData();
            data.setPlatforms(Collections.singletonList(platformData));

            return HotSearchResponse.success(data);
        } catch (Exception e) {
            return HotSearchResponse.error("搜索失败: " + e.getMessage());
        }
    }

    /**
     * 清除缓存
     */
    @PostMapping("/clear-cache")
    public HotSearchResponse clearCache() {
        try {
            hotSearchManager.clearCache();
            return HotSearchResponse.success("缓存清除成功");
        } catch (Exception e) {
            return HotSearchResponse.error("清除缓存失败: " + e.getMessage());
        }
    }
}
